package com.spring.boot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spring.boot.entity.SysUserRole;
import com.spring.boot.mapper.SysUserRoleMapper;
import com.spring.boot.service.SysUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("sysUserRoleServiceImpl")
public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUserRole> implements SysUserRoleService {
    @Autowired
    private SysUserRoleMapper userRoleMapper;
    @Override
    public List<SysUserRole> getByUserId(int id) {
        QueryWrapper<SysUserRole> urWrapper = new QueryWrapper<>();
        urWrapper.eq("user_id",id);
        List<SysUserRole> userRoleList = userRoleMapper.selectList(urWrapper);
        return userRoleList;
    }

    @Override
    public void updateByUserId(List<SysUserRole> userRoleList) {
        //根据userId取到uerRoleId
        List<SysUserRole> userRoleIds = super.list(new QueryWrapper<SysUserRole>().eq("user_id", userRoleList.get(0).getUserId()));
        if(userRoleIds.size() == userRoleList.size()){
            whenNewEqOld(userRoleList,userRoleIds);
        } else if(userRoleIds.size() < userRoleList.size()){//如果新的大于旧的，就新增
            whenNewGtOld(userRoleList,userRoleIds);
        } else { //如果新的小于旧的，删掉多出来的旧的
            whenNewLtOld(userRoleList,userRoleIds);
        }
    }

    @Override
    public void insertByUserId(int userId, ArrayList<Integer> roles) {
        for (Integer roleId : roles) {
            userRoleMapper.insert(new SysUserRole(userId,roleId));
        }
    }

    /**
     * 新等于旧
     * @param userRoleList
     * @param userRoleIds
     */
    private void whenNewEqOld(List<SysUserRole> userRoleList, List<SysUserRole> userRoleIds){
        for (int i = 0; i < userRoleIds.size(); i++) {
            int id = userRoleIds.get(i).getId();
            //根据userRole id 更新
            super.update(new UpdateWrapper<SysUserRole>().lambda()
                    .eq(SysUserRole::getId,id)
                    .set(SysUserRole::getRoleId,userRoleList.get(i).getRoleId())
                    .set(SysUserRole::getUserId,userRoleList.get(i).getUserId()));
        }
    }

    /**
     * 新大于旧
     * @param userRoleList
     * @param userRoleIds
     */
    private void whenNewGtOld(List<SysUserRole> userRoleList, List<SysUserRole> userRoleIds){
        for (int i = 0; i < userRoleList.size(); i++) {
            if(i >= userRoleIds.size()){//判断出查到的id不够用了
                userRoleMapper.insert(new SysUserRole(userRoleList.get(i).getUserId(),userRoleList.get(i).getRoleId()));
            }else{
                //根据userRole id 更新
                super.update(new UpdateWrapper<SysUserRole>().lambda()
                        .eq(SysUserRole::getId,userRoleIds.get(i).getId())
                        .set(SysUserRole::getRoleId,userRoleList.get(i).getRoleId())
                        .set(SysUserRole::getUserId,userRoleList.get(i).getUserId()));
            }
        }
    }

    /**新小于旧
     * @param userRoleList
     * @param userRoleIds
     */
    private void whenNewLtOld(List<SysUserRole> userRoleList, List<SysUserRole> userRoleIds){
        for (int i = 0; i < userRoleIds.size(); i++) {
            if(i >= userRoleList.size()){
                userRoleMapper.deleteById(userRoleIds.get(i).getId());
            }else{
                //根据userRole id 更新
                super.update(new UpdateWrapper<SysUserRole>().lambda()
                        .eq(SysUserRole::getId,userRoleIds.get(i).getId())
                        .set(SysUserRole::getRoleId,userRoleList.get(i).getRoleId())
                        .set(SysUserRole::getUserId,userRoleList.get(i).getUserId()));
            }
        }
    }
}
